<?php
class Brand extends CI_Controller{
	public function __construct()
    {
		parent::__construct();
		$this->load->model('brand_model');
	}
	function index($start = 0){
		$this->load->helper('cookie');
		$limit = 10;
		// get all brand
		$result = $this->brand_model->get_all_brand($limit,$start);
		$data['all_brand'] = $result['rows'];
		$data['all'] = $result['all'];
		// post ajax
		if($this->input->get('action')=='edit'){
			$this->input->set_cookie('brand_id',$this->input->post('brand_id'),0);
			echo $this->brand_model->get_brand_by_id($this->input->post('brand_id'));	
		}else{
			// pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url()."/brand/index/";
			$config['total_rows'] = $result['totalRows'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 3;
			$config['num_links'] = 3;
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();	
			$this->load->view('brand_view',$data);
		}
	  }
	// add new brand
	function add(){
		$data['brand_alias'] = $this->input->post('brand_code');
		$str_brand_name 	 = $this->input->post('brand_name'); 
		$data['brand_name']  = str_replace(' ','',$str_brand_name);
		$data['brand_des']   = $this->input->post('brand_des');
		$data['status']      = $this->input->post('status');
		if(!$this->brand_model->check_brand_code($this->input->post('brand_code'))){
			$this->brand_model->add_brand($data);
		}else{
			echo '<script> alert("Brand code already exists ");</script>';	
		}
		redirect('/brand/index'); // redirect to index page of brand module // remove after using ajax grid & pagination
	}
	// edit brand
	function edit(){
		$id = $this->input->cookie('brand_id');
		$data['brand_alias'] = $this->input->post('brand_code');
		$str_brand_name 	 = $this->input->post('brand_name'); 
		$data['brand_name']  = str_replace(' ','',$str_brand_name);
		$data['brand_des']   = $this->input->post('brand_des');
		$data['status']      = $this->input->post('status');
		$this->brand_model->edit_brand($id,$data);
        redirect('/brand/index'); // redirect to index page of brand module // remove after using ajax grid & pagination
	}
	// delete brand
	function delete(){
		$id_delete = $this->input->post('selected_box');
		$this->brand_model->delete_brand($id_delete);
        redirect('/brand/index'); // redirect to index page of brand module // remove after using ajax grid & pagination
	}
}